<template>
  <div>
    <el-card style="margin: 20px 0">
      <categoryselect @getCategorys="getCategorys" :show="flag"></categoryselect>
    </el-card>
    <el-card>
      <div v-if="flag">
        <el-button
          type="primary"
          :disabled="!idList.category3Id"
          @click="addAttr"
          >添加</el-button
        >
        <el-table :data="infoList" style="width: 100%">
          <el-table-column type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="attrName" label="属性名称"> </el-table-column>
          <el-table-column prop="attrValueList" label="属性值">
            <template slot-scope="{ row, $index }">
              <button
                v-for="i in row.attrValueList"
                :key="i.id"
                style="margin: 0 4px"
              >
                {{ i.valueName }}
              </button>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作">
            <template slot-scope="{ row, $index }">
              <el-button type="warning" @click="updateAttrValue(row)"
                >修改</el-button
              >
              <el-button type="danger">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 添加属性后显示的内容区域 -->
      <div v-else>
        <el-form
          status-icon
          label-width="100px"
          class="demo-ruleForm"
          :model="attrInfo"
        >
          <el-form-item label="属性名" style="width: 280px">
            <el-input autocomplete="off" v-model="attrInfo.attrName"></el-input>
          </el-form-item>
          <el-button
            type="primary"
            @click="addAttrValue"
            :disabled="!attrInfo.attrName"
            >+添加属性值</el-button
          >
          <el-button>取消</el-button>
          <el-table
            style="width: 100%; margin: 25px 0"
            :data="attrInfo.attrValueList"
            border
          >
            <el-table-column
              type="index"
              label="序号"
              width="80"
              align="center"
            >
            </el-table-column>
            <el-table-column label="属性值名称" prop="valueName">
              <template slot-scope="{ row, $index }">
                <el-input
                  v-model="row.valueName"
                  placeholder="请输入属性值名称"
                  size="mini"
                  :ref="$index"
                  v-if="row.valueFlag"
                  @blur="toLook(row)"
                  @keyup.native.enter="toLook(row)"
                ></el-input>
                <div v-else @click="toEdit(row, $index)">
                  {{ row.valueName }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="{ row, $index }">
                <el-popconfirm
                  confirm-button-text="确定"
                  cancel-button-text="取消"
                  icon="el-icon-info"
                  icon-color="red"
                  :title="`确定删除${row.valueName}吗？`"
                   @onConfirm="deleteAttr($index)"
                >
                  <el-button
                  type="danger"
                  slot="reference"
                  icon="el-icon-delete"
                ></el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <el-button type="primary" @click="saveAttr" :disabled="attrInfo.attrValueList.length<1">保存</el-button>
          <el-button @click="flag = 'true'">取消</el-button>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import cloneDeep from "lodash/cloneDeep";
export default {
  name: "Attr",
  data() {
    return {
      infoList: [],
      idList: [],
      flag:true,
      attrInfo: {
        attrName: "",
        attrValueList: [],
        categoryId: 0,
        categoryLevel: 3,
      },
    };
  },
  methods: {
    getCategorys(Ids) {
      this.idList = Ids;
      this.getInfoList();
    },
    async getInfoList() {
      const { category1Id, category2Id, category3Id } = this.idList;
      let result = await this.$API.attr.getAttrInfoList(
        category1Id,
        category2Id,
        category3Id
      );
      console.log(result, 111111111111);
      if (result.code == 200) {
        this.infoList = result.data;
      }
    },
    addAttr() {
      this.flag = false;
      this.attrInfo = {
        attrName: "",
        attrValueList: [],
        categoryId: this.category3Id,
        categoryLevel: 3,
      };
    },
    addAttrValue() {
     this.attrInfo.attrValueList.push({
        attrId: this.attrInfo.id,
        valueName: "",
        valueFlag: true,
      });
      this.$nextTick(() => {
        this.$refs[this.attrInfo.attrValueList.length - 1].focus();
      });
    },
    updateAttrValue(row) {
      this.flag = false;
      this.attrInfo = cloneDeep(row);
      this.attrInfo.attrValueList.forEach((item) => {
        // item.valueFlag=false 不能这么写！！！！
        this.$set(item, "valueFlag", false);
      });
      console.log(this.attrInfo, 1222333);

    },
    toLook(row) {
      if (row.valueName.trim() == "") {
        this.$message("请输入非空属性名");
        return;
      }
      let isrepat = this.attrInfo.attrValueList.some((item) => {
        if (row !== item) {
          return row.valueName == item.valueName;
        }
      });
      if (isrepat) return;
      row.valueFlag = false;
    },
    toEdit(row, $index) {
      console.log(77777777777777, row);
      row.valueFlag = true;
      this.$nextTick(() => {
        this.$refs[$index].focus();
      });
    },
    deleteAttr(index) {
      this.attrInfo.attrValueList.splice(index, 1);
    },
   async saveAttr(){
      this.attrInfo.attrValueList=this.attrInfo.attrValueList.filter((item)=>{
         if(item. attrName!=""){
          delete item.valueFlag
          return true
         }
      })
      try {
        console.log(999999999999,this.attrInfo);
        await this.$API.attr.saveOrUpdateAttrInfo(this.attrInfo)
        this.flag=true
        this.$message({type:'success',message:"保存成功"})
        this.getInfoList()
    
      } catch (error) {
        this.$message("保存失败")
      }
    }
  },
};
</script>

<style>
</style>
